package yf.mall.controller;

import java.io.IOException;
import java.util.List;

import com.yf.response.RRException;
import com.yf.utils.ShiroUtils;
import org.springframework.web.multipart.MultipartFile;
import yf.mall.common.Const;
import yf.mall.entity.OrderItemEntity;
import com.yf.request.SearchParam;
import yf.mall.enums.MallOrderBusTypeEnum;
import yf.mall.service.OrderItemService;
import yf.mall.service.SysUsersService;
import yf.mall.vo.MallOrderVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;

import java.util.Map;
import java.util.stream.Collectors;

import yf.mall.entity.MallOrderEntity;
import yf.mall.service.MallOrderService;
import com.yf.response.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 商品订单Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Aug 13 14:26:20 CST 2019
 */
@RestController
@RequestMapping("/mallOrder")
public class MallOrderController {
    @Autowired
    private MallOrderService mallOrderService;

    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private SysUsersService sysUsersService;

    /**
     * 列表商品订单
     */
    @RequestMapping("/list")
    @RequiresPermissions("mall:order:list")
    public R list(@RequestBody SearchParam<MallOrderVo> params) {
        Long sysUserId = ShiroUtils.getUserId();
        Long userId = sysUsersService.findBySysUserId(sysUserId);
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());

        List mallOrderList;
        if (userId != null){
            //获取绑定用户的订单记录
           mallOrderList = mallOrderService.queryListVo(params.getEntity(),userId);
        }else {
            //获取公司的订单记录
            mallOrderList = mallOrderService.queryListVo(params.getEntity(),null);
        }
        return R.ok(new PageInfo(mallOrderList));
    }


    /**
     * 折让
     */
    @RequestMapping("/profit")
    @RequiresPermissions("mall:order:list")
    public void levelProfit(@RequestBody SearchParam<MallOrderVo> params,HttpServletResponse httpServletResponse) {
        Long sysUserId = ShiroUtils.getUserId();
        if(!sysUserId.equals(1L)){
            throw new RRException("没有权限");
        }
        List<MallOrderVo> list =  mallOrderService.queryListVo(params.getEntity(),null);
        List<MallOrderEntity> tems = list.stream().map(m->m.getMallOrder()).collect(Collectors.toList());
        mallOrderService.downLevelProfit(tems,httpServletResponse);
    }

    /**
     * 查看商品订单
     */
    @SysLog("查看商品订单")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:order:info")
    public R info(@PathVariable("id") Long id) {
        MallOrderVo mallOrder = mallOrderService.getVo(id);
        List<OrderItemEntity> itemEntities = orderItemService.findByOrderId(id);
        mallOrder.getMallOrder().setItems(itemEntities);
        return R.ok(mallOrder);
    }

    /**
     * 保存商品订单
     */
    @SysLog("保存商品订单")
    @RequestMapping("/save")
    @RequiresPermissions("mall:order:save")
    public R save(MallOrderEntity mallOrder) {
        mallOrderService.save(mallOrder);
        return R.ok(mallOrder);
    }

    /**
     * 修改商品订单
     */
    @SysLog("修改商品订单")
    @RequestMapping("/update")
    @RequiresPermissions("mall:order:update")
    public R update(MallOrderEntity mallOrder) {

        mallOrderService.updateOrder(mallOrder);
        return R.ok(mallOrder);
    }

    /**
     * 删除商品订单
     */
    @SysLog("删除商品订单")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:order:delete")
    public R delete(Long[] ids) {
        mallOrderService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 所有商品订单
     */
    @SysLog("所有商品订单")
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<MallOrderEntity> mallOrderList = mallOrderService.queryList(params);
        return R.ok(mallOrderList);
    }


    /***********自定义开始***********/

    @RequestMapping("/items")
    public R getItems(Long id) {
        MallOrderEntity mallOrderEntity = mallOrderService.get(id);
        List<OrderItemEntity> orderItemEntities = orderItemService.findByOrderId(id);
        mallOrderEntity.setItems(orderItemEntities);
        return R.ok(mallOrderEntity);
    }

    /**
     * 管理端转货
     *
     * @param mallOrderVo
     * @return
     */
    @SysLog("转货")
    @RequestMapping("/transfer")
    @RequiresPermissions("user:order:transfer")
    public R transfer(@RequestBody MallOrderVo mallOrderVo) {
        mallOrderService.transfer(mallOrderVo);
        return R.ok();
    }

    /**
     * 下载订单
     *
     * @param request
     * @param response
     */
    @SysLog("订单下载")
    @RequestMapping("/download")
    @RequiresPermissions("mall:order:wuliuDownLoad")
    public void download(HttpServletResponse response) {
        mallOrderService.downLoad(response);
    }

    /**
     * 下载订单
     *
     * @param request
     * @param response
     */
    @SysLog("订单下载")
    @RequestMapping("/downloadOrder")
    @RequiresPermissions("mall:order:downLoad")
    public void downloadOrder(HttpServletResponse response) {
        mallOrderService.downLoadOrder(response);
    }

    /**
     * 上传订单
     *
     * @param file
     */
    @RequestMapping("/upload")
    @RequiresPermissions("mall:order:upLoad")
    public void upload(MultipartFile file) {
        try {
            mallOrderService.upload(file.getInputStream());
        } catch (IOException e) {
            throw new RRException("上传的文件有误!");
        }
    }

    /**
     * 恢复订单
     */
    @RequestMapping("/renew")
    @RequiresPermissions("mall:order:renew")
    public void renew() {
        mallOrderService.renew();
    }


    /**
     * 管理员直接支付
     *
     * @param orderId
     */
    @RequestMapping("/pay")
    @RequiresPermissions("mall:order:pay")
    public R pay(Long orderId) {
        MallOrderEntity mallOrderEntity = mallOrderService.adminPay(orderId);
        return R.ok(mallOrderEntity);
    }

    /**
     * 删除订单
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/close")
    @RequiresPermissions("mall:order:cancel")
    public R closeOrder(Long orderId) {
        MallOrderEntity mallOrderEntity = mallOrderService.deleteOrder(Const.ADMIN_ID, orderId);
        return R.ok(mallOrderEntity);
    }
    /**
     * 提走订单
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/mention")
    @RequiresPermissions("mall:order:mention")
    public R mentionOrder(Long orderId) {
        MallOrderEntity mallOrderEntity = mallOrderService.mentionOrder(orderId);
        return R.ok(mallOrderEntity);
    }

}
